home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Environments / CGIshell 1.3.2 / documentation / shell-docs.txt
Text File  |  1996-04-23  |  18KB  |  567 lines

  1.  
  2. CGI shell, version 1.3.2
  3.  
  4. (This document replaces the old documentation for versions 1.1, 1.2, 1.3,1.3.1)
  5.  
  6. This documentation, the software it describes, and the source code included
  7. with this package is:
  8.  
  9. Copyright, Ronald T. Kneusel, 1995, 1996.  All Rights Reserved.
  10. rkneusel@post.its.mcw.edu
  11.  
  12. Distribution permitted if the entire archive is intact.  If you want to
  13. put this on CD-ROMs then you need to contact me first.
  14.  
  15. ---------------------------------------------------------------------------------
  16.  
  17. WHAT'S NEW
  18.  
  19.  
  20. 1.3.2  (23-Apr-96):  Rewrote @Field to make it more stable.
  21.  
  22. 1.3.1  (17-Apr-96):  Added #digits variable to control number of decimal places
  23.                      when !val stores a floating point number.  Default 6.
  24.               
  25.               
  26. ---------------------------------------------------------------------------------
  27.  
  28.  
  29. *****************************
  30. *                           *
  31. *      INTRODUCTION         *
  32. *                           *
  33. *****************************
  34.  
  35.  
  36. DISCLAIMER
  37.  
  38.  
  39.     This is a tool for programmers; Forth programmers in particular.  Therefore,
  40. this documentation is intentionally sparse.  After all, real programmers don't
  41. need no steenkin' documentation, right? :)
  42.  
  43.  
  44.  
  45. WHAT IS IT?
  46.  
  47.     CGI shell works with Chris Heilman's Pocket Forth programming language to 
  48. provide a skeleton for creating custom CGI applications for WebSTAR/MacHTTP.  
  49. The shell code handles AppleEvents, the interpretation of forms data, and
  50. supplies a set of words for building a reply string.  Pocket Forth, version 6.5
  51. is included in the release.
  52.  
  53.  
  54.  
  55. WHAT DO I NEED TO RUN IT?
  56.  
  57.     • A Mac running WebSTAR (commercial) or MacHTTP (shareware)
  58.     • This code
  59.     • A text editor (Teachtext or Simpletext will do in a pinch)
  60.     • Some Forth and HTML programming experience
  61.     
  62.  
  63.  
  64. ABOUT THE SHELL SOURCE CODE
  65.  
  66.     There are two layers to the shell code.  The file  SERVER.4TH  contains
  67. a minimal web server interface.  Only the simplest of CGI applications will
  68. use this file alone.  The second, more powerful, layer is contained in the
  69. files: STRING.4TH, FIELD.4TH, and TEMPLATE.4TH.  Unless the CGI you wish to
  70. create is exceedingly simple it is best to use the SHELL.4TH file as it contains
  71. all the files above in the proper order.
  72.  
  73.    The remainder of this file is split into two parts:  SERVER.4TH and SHELL.4TH
  74. which describe the low-level and high-level interface code respectively.
  75.  
  76.  
  77.  
  78. CONTENTS
  79.  
  80.  
  81.    • Part 1 --  Low level routines 
  82.    
  83.    • Part 2 --  High level routines
  84.  
  85.  
  86. ================================================================================
  87.  
  88.  
  89. *************************************
  90. *                                   *
  91. *  Part 1  SERVER.4TH - low level   *
  92. *                                   *
  93. *************************************
  94.  
  95.  
  96.  
  97. A FIRST EXAMPLE
  98.  
  99.  
  100. A minimal CGI using the SERVER.4TH file alone is:
  101.  
  102.  
  103.    \ Simple CGI
  104.    
  105.    --> server.4th    ( load only the minimal CGI code )
  106.     
  107.    message[ s1 You are: ]                 ( strings used in the reply )
  108.    message[ s2 <br>Using: ]
  109.    message[ s3 <br>And your favorite flavor is: ]
  110.    
  111.    message[ fname flavor]        ( name of the field )
  112.    
  113.    512 String>> out    ( return string goes here, 512 char max )
  114.    out newstr          ( clear it )
  115.    
  116.    ,s sdoc  ,s WWWΩ  ae:   ( begin the AppleEvent reply handler )
  117.    
  118.       out startstring          ( add HTML header )
  119.       
  120.       s1 out strcpy            ( add 'You are: ' )
  121.       out APPEND @Addr         ( add client's IP address )
  122.       
  123.       s2 out strcpy            ( add '<br>Using: ' )
  124.       out APPEND @Browser      ( add browser type  )
  125.       
  126.       s3 out strcpy            ( add '<br>And your favorite flavor is: ' )
  127.       out fname APPEND @Field  ( add flavor field data )
  128.       
  129.       out endstring            ( add HTML ending )
  130.       
  131.       out REPLY  bye           ( send the reply and quit )
  132.       
  133.    ;ae                    ( end reply handler )
  134.    
  135.    
  136.  
  137. This CGI will respond to an HTML form like this one:
  138.  
  139.  
  140.    <h1> Simple CGI test </h1>
  141.    <form method=post action="simple.cgi">
  142.      What is your favorite ice cream flavor?
  143.      <select name="flavor"
  144.        <option>Vanilla <option>Chocolate <option>Strawberry
  145.      </select>
  146.      <input type=submit value="Send it">
  147.    </form> 
  148.    
  149.    
  150. To create 'simple.cgi' load the Forth code above into a *copy* of Pocket Forth
  151. and then enter  save bye  to save the dictionary.  Use a web browser to bring
  152. up the HTML file, call it 'simple.html', and click 'send it'.  If all went well
  153. you will receive a reply indicating who you are, what you are using, and 
  154. (presumably) your favorite type of ice cream.
  155.  
  156.  
  157.  
  158. MAIN USER LEVEL WORDS
  159.  
  160.  
  161.     The most important words defined by SERVER.4TH are @Direct, @Addr, @Browser,
  162. and @Field.  These words fetch information sent by the web server to the CGI
  163. application.
  164.  
  165.  
  166.  @Direct  (e.g.  out APPEND @Direct )
  167.  
  168.     Direct returns the direct argument attached to the action component of the
  169.     form definition.  If the HTML file contains:
  170.     
  171.         <form method=post action="my.cgi$3.141592">
  172.         
  173.     Then the phrase  out APPEND @Direct  will append the string 3.141592 to
  174.     the string contained in  out  .  If NEW is specified in place of APPEND
  175.     the direct argument replaces the previous contents of the string.  All
  176.     strings are terminated with a '\0' character.
  177.     
  178.     
  179.  @Addr  (e.g.   out APPEND @Addr )
  180.   
  181.     Addr returns the IP address string of the client.  If the address has a
  182.     DNS entry its name will be returned.  If there was no DNS entry the reversed
  183.     in-arpa numerical IP address is returned.  NEW and APPEND work as above.
  184.     
  185.     
  186.  @Browser  (e.g.  out NEW @Browser )
  187.  
  188.     Browser returns a string indicating what type of web browser the client
  189.     is using.  This can be useful for deciding when to return a graphically
  190.     intensive page and when not to.  @Browser works as @Addr and @Direct.
  191.     
  192.     
  193.  @Field  (e.g.  out f1 APPEND @Field )
  194.  
  195.     Field places the contents of the field whose name is in the string  f1
  196.     into the string  out  .  NEW and APPEND work as before.  @Field will handle
  197.     all translation of characters.
  198.     
  199.     
  200.  REPLY   (e.g.  out REPLY )
  201.  
  202.     REPLY returns the string on the stack to the web server which then sends
  203.     it down to the client.  This word must be called only from within a
  204.     ae: ... ;ae pair.
  205.     
  206.     
  207. SETTING UP THE APPLE EVENT HANDLER
  208.  
  209.   The heart of a CGI application is the AppleEvent handler that responds to the
  210.   AppleEvent sent by the web server.  In Pocket Forth an AppleEvent handler is
  211.   first defined and then saved to the dictionary.  When the application is then
  212.   launched the handler is added to the list of handlers and the application 
  213.   will respond to the event whenever it is listening for events.  Pocket Forth
  214.   listens to system events when waiting for input so it is unnecessary to
  215.   be concerned about handling local events (i.e. mouse clicks, keypresses, etc.)
  216.   In fact, Pocket Forth can be used normally while still listening for
  217.   AppleEvents.  If you write the CGI to quit when finished you do not need to
  218.   write any code for dealing with the server Mac.  This is unlike the C based
  219.   Responder CGI shell.
  220.   
  221.   In Pocket Forth an Apple Event handler is defined using ae: and ;ae,
  222.   
  223.   ,s sdoc  ,s WWWΩ  ae:   \ begin the handler for the sdoc,WWWΩ event
  224.   
  225.      ...some code here... 
  226.      
  227.   ;ae
  228.   
  229.   The general process, then, is:
  230.   
  231.   ,s sdoc  ,s WWWΩ  ae:
  232.   
  233.     <get data from the apple event using @Direct, @Field, etc.>
  234.     
  235.     <manipulate the data and create an output string>
  236.     
  237.     <reply to the event with  <output-string> REPLY>
  238.     
  239.     <quit or re-initialize the application>
  240.     
  241.   ;ae
  242.   
  243.   See the included examples for more information.
  244.   
  245.   
  246. STRING SUPPORT WORDS
  247.  
  248.   The CGI shell provides a number of words for manipulating null terminated 
  249. strings.  These are summarized below:
  250.  
  251.  
  252.    message[  ( compile: <name> <text>] )
  253.              ( run: -- address )
  254.              
  255.              Create <name>, a null terminated string.  E.g.
  256.                message[ s0 <h1>Result...</h1>]
  257.    
  258.    String>>  ( size -- (<name>) )
  259.    
  260.              Create a string, <name>, <size> bytes long.  E.g.
  261.                512 String>> theReply
  262.                
  263.    strcpy    ( s1 s2 -- )
  264.    
  265.              Append s1 to s2.  Appending is useful for creating reply
  266.              strings, use  strncpy  to avoid appending.
  267.              
  268.    length    ( s -- n )
  269.    
  270.              Return the length of string s.
  271.              
  272.    newstr    ( s -- )
  273.    
  274.              Zero the string s.  Equal to  0 s c!
  275.              
  276.    0type     ( s -- )
  277.    
  278.              Type the null terminated string s on the screen.  Useful for
  279.              debugging.
  280.              
  281.    accept    ( s length -- )
  282.    
  283.              Identical to  expect  but adds a terminating '\0'.
  284.              
  285.    startstring  ( s -- )
  286.    
  287.              Add <HTML> to the string s.
  288.              
  289.    endstring ( s -- )
  290.    
  291.              Append </HTML> to the string s.
  292.              
  293.              
  294. NUMERICAL CONVERSION
  295.  
  296.    Pocket Forth has complete support for floating point numbers making it useful
  297.    for scientific applications.  These words will convert between floating point
  298.    numbers and strings.
  299.    
  300.    
  301.    f>str  ( f s -- )
  302.    
  303.           Put the floating point number f into the string using the current
  304.           settings for FIX or SCI.
  305.           
  306.    str>f  ( s -- f )
  307.    
  308.           Make s into a floating point number on the stack.
  309.           
  310.  
  311.  
  312.  
  313. *************************************
  314. *                                   *
  315. *  Part 2  SHELL.4TH - high level   *
  316. *                                   *
  317. *************************************
  318.  
  319.  
  320. This is where the real power lies.  The code in SHELL.4TH allows you to create
  321. an external file with all the HTML to be returned and to place markers in the
  322. file to indicate where to substitute values (strings or numbers).  This 
  323. separates the HTML source from the program itself and allows you to alter the
  324. appearance without recompiling the application.  It also save valuable 
  325. dictionary space by not including a lot of text.
  326.  
  327. Say we have a web page that looks like this:
  328.  
  329.      <form method=post action="ftest.cgi">
  330.      Name? <input type="text" name="name"><p>
  331.      Age? <input type="text" name="age"><p>
  332.      Weight? <input type="text" name="weight">
  333.      <p><input type=submit value="Go">
  334.      </form>
  335.   
  336. This creates a form that asks for three values:
  337.  
  338.      - a name   (a string)
  339.      - an age   (an integer)
  340.      - a weight (floating point)
  341.      
  342. It then calls a CGI application named 'ftest.cgi' which looks like this:
  343.  
  344.  
  345.      \ simple CGI
  346.      
  347.      --> shell.4th                     \ load CGI shell code
  348.      
  349.      $[ file ftest.txt]                \ name of HTML code file
  350.      
  351.      2048 String>> out                 \ reply goes in here
  352.      
  353.      STR 80 " name"     #field f1      \ define the name field
  354.      INT 15 " age"      #field f2      \ and the age field
  355.      FP  20 " weight"   #field f3      \ and the weight field
  356.      STR 80 " a_string" #object s1     \ plus an extra string (no reason)
  357.      
  358.      message[ ss Get a life, Bozo!]    \ a constant string
  359.      
  360.      ,s sdoc ,s WWWΩ ae:               \ start the AppleEvent reply
  361.        <getFields>                     \ automatically load field data
  362.        ss s1 !val                      \ put the text in the s1 object
  363.        out file NEW template           \ call template to fill in reply
  364.        out REPLY                       \ send the reply
  365.        bye                             \ and quit
  366.      ;ae                       
  367.  
  368.  
  369. Which uses the template word to read the file 'ftest.txt':
  370.  
  371.  
  372.     <body bgcolor="#ffffff">
  373.     <font color="#ff0000">
  374.     <h1>The Big Cheese</h1>
  375.     <hr><font size=+2 color="#339944">
  376.     You are: <b>`name`</b> <p>
  377.     You are <b>`age`</b> years old. <p>
  378.     You weigh <b>`weight`</b> pounds.
  379.     </font>
  380.     <h2>`a_string`</h2>
  381.  
  382.  
  383. Notice the values in ` (backquotes)?  These are the names of the fields 
  384. and objects which will have their values substituted in the reply.
  385.  
  386.  
  387.  
  388. WHAT'S THIS 'FIELD' AND 'OBJECT' BUSINESS?
  389.  
  390.  
  391. There is only one difference between a FIELD and an OBJECT: fields are
  392. automatically entered in the field array upon their definition.  This 
  393. array is used by <getFields> to load the field data from the Apple Event.  
  394. Almost all of your CGIs will start with a call to <getFields>.
  395.  
  396. There is a second array, called the template array, which holds pointers to 
  397. all the items that are within the backquotes in the template file.  
  398. Fields are entered in this array, as are objects, when they are defined.
  399.  
  400. Three types of Objects/Fields exist: string, integer, and float.  The 
  401. general memory structure for an object is:
  402.  
  403.  
  404.   +----+--------+-----------+---------------+
  405.   |type| name   | value ... | text ........ |
  406.   +----+--------+-----------+---------------+
  407.  
  408.  
  409. where:
  410.  
  411.   type  (1 byte)       =  0 STR, 1 INT, 5 FP
  412.   name  (30 bytes)     =  null terminated text of field name
  413.   value (0,2,10 bytes) =  value of field, for STR is same as start of text, 
  414.                           2 bytes for INT, 10 bytes for FP
  415.   text  (varies)       =  text string of value, i.e. INT is 2 then text is "2"
  416.  
  417.  
  418. When the fields are loaded from the Apple Event they are automatically 
  419. filled in according to their type.  Values stored in the field are also 
  420. automatically stored as the proper type (for Forth) and as a text string 
  421. (for the web server).
  422.  
  423. The template handles looking up names in the template file and 
  424. substituting the right text, you do not need to be at all concerned about 
  425. it.  This is a big improvement over the array fiddling necessary in the 
  426. previous version.
  427.  
  428.  
  429.  
  430. USING OBJECTS/FIELDS
  431.  
  432.  
  433. Part of the goal in this new version is to make transparant the necessary 
  434. switching between numerical values and strings.  The other major goal was 
  435. to remove the array fiddling and let the programmer have easy access to 
  436. the template file.
  437.  
  438. Let's define a field:
  439.  
  440.    FP 20 " spin" #field sp     ( spin quantum number )
  441.  
  442. We have created a field named 'spin' that acts like a variable, i.e. 
  443. referencing  sp  returns the address of the beginning of the field.  So, 
  444. what is it:
  445.  
  446.    FP      --  floating point field
  447.    20      --  string part is 20 characters long (no bounds checks!)
  448.    " spin" --  the name part is 'spin'
  449.    sp      --  the dictionary name (Forth) for this object
  450.    
  451. The parts of the record are accessed via:
  452.  
  453.    sp .type    -- push field type on the stack (0=STR,1=INT,5=FP)
  454.    
  455.    sp .name    -- push address of beginning of name field on stack
  456.    
  457.    sp .val     -- address of value part
  458.    
  459.    sp .text    -- address of the text part
  460.    
  461.  
  462. Values are read from and written to the field from within the program by:
  463.  
  464.   @val   --  return the current value of the field:
  465.                      -- address of text part if it is a string
  466.                      -- 16-bit integer if of type INT
  467.                      -- floating point number if of type FP
  468.   
  469.   !val   --  store in the current record, if string then copy, if a 
  470.              number then store in value part and then store a string
  471.              of the number in the text part.
  472.              
  473.              
  474. ** Note:  The number of decimal places !val uses when storing a floating
  475.           point number is determined by the value of the variable #digits
  476.           The default is 6,  3 #digits !  would set it to three.
  477.              
  478.              
  479. Example, using the field above:
  480.  
  481.   1.5 sp !val   
  482.       ( put 1.5 in the value part and "1.5" in the string part)
  483.   
  484.   sp @val 2.0 f* f.
  485.       ( get the value part, 1.5, and multiply by 2, prints 3.0)
  486.  
  487.  
  488. This transparent conversion between string and number when coupled with 
  489. the automatic initialization and substitution of values is what makes the 
  490. full CGI shell useful.
  491.  
  492.  
  493.  
  494. TEMPLATES
  495.  
  496.  
  497. The word TEMPLATE accepts an output string and a file name.  It then 
  498. reads the file into the output string substituting the text part of 
  499. FIELDs and OBJECTs whose names are delimited within the file by ` 
  500. (backquotes).
  501.  
  502. Unlike the previous version of the CGI shell, there is nothing else for 
  503. the programmer to do. TEMPLATE does all the searching for matches between 
  504. the name read from the file and the names of fields and objects that were 
  505. stored when defined.
  506.  
  507. Specifically:
  508.  
  509.   
  510.     template  ( output-string-address  filename-addr  NEW|APPEND )
  511.                                                        -1   0
  512.  
  513. NEW and APPEND are constants defined in SERVER.4TH.  If NEW the output 
  514. string is cleared before use, if APPEND the template file is appened to 
  515. the end of the existing string.
  516.  
  517. The filename string *MUST* be defined with the word $[ as it needs to be 
  518. a counted string and not a null terminated string:
  519.  
  520. $[ f1 ftest.txt]     \  ] ends the text, no spaces
  521. 2048 String>> out    \  a 2k output buffer
  522.  
  523. out f1 NEW template  \  fill the string with the text in ftest.txt
  524.  
  525.  
  526. It's that easy!  Look at the included sample code to learn more.
  527.  
  528.  
  529. =================================================================================
  530.           
  531.           
  532. NOTES
  533.  
  534.    The best way to learn to use the shell is to experiment.  See the 'Deflect'
  535. example to learn how to force the client's browser to another URL.  When 
  536. developing a CGI keep the following in mind:
  537.  
  538.  
  539.    *  turn off the processor cache if using an '040 based Mac.  Some code will
  540.       compile with it on but often the application quits part way through. The
  541.       compiled code runs just fine with the cache on.
  542.  
  543.       
  544.    *  always use a fresh copy of the Pocket Forth application when re-compiling.
  545.    
  546.    
  547.    *  remember to do  save bye  after loading the code.  Amazing how many times
  548.       I forget to do that!
  549.       
  550.       
  551. The software carries no warranty of any kind.  If it blows up and kills 
  552. your hard drive, don't come whining to me about it. (It won't, no worries :) 
  553. Caveat Emptor.
  554.  
  555. Ron Kneusel
  556. 8725 West Burdick Ave.
  557. Milwaukee, WI  53227  USA
  558. (414) 545-7557
  559. rkneusel@post.its.mcw.edu
  560.  
  561. The latest version of CGIshell can always be found at:
  562.  
  563. http://kreeft.intmed.mcw.edu/pf.html
  564. ftp://kreeft.intmed.mcw.edu/q/pub/forth/
  565.  
  566. March 1996, AMDG
  567.